//
//  LXBottomToolView.m
//  绿稀全球购
//
//  Created by 诸葛孔明 on 16/6/3.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "ShopCartBottomToolView.h"

@implementation ShopCartBottomToolView



- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        UIButton *selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [selectedButton setImage:[UIImage imageNamed:@"shoppingcart_but_n"] forState:UIControlStateNormal];
        [selectedButton setImage:[UIImage imageNamed:@"shoppingcart_but_s"] forState:UIControlStateSelected];
        [selectedButton addTarget:self action:@selector(selectedButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        self.selectedButton = selectedButton;
        [self addSubview:selectedButton];

        
        
        UILabel *allSelectedLabel = [[UILabel alloc] init];
        allSelectedLabel.text = @"全选";
        allSelectedLabel.font = [UIFont systemFontOfSize:14];
        allSelectedLabel.textAlignment = NSTextAlignmentCenter;
        self.allSelectedLabel = allSelectedLabel;
        [self addSubview:allSelectedLabel];
        
        
        UILabel *totalLabel = [[UILabel alloc] init];
        totalLabel.text = @"合计:";
        totalLabel.font = [UIFont systemFontOfSize:12];
        totalLabel.textAlignment = NSTextAlignmentCenter;
        self.totalLabel = totalLabel;
        [self addSubview:totalLabel];
        
        UILabel *priceLabel = [[UILabel alloc] init];
        priceLabel.font = [UIFont systemFontOfSize:14];
        priceLabel.textAlignment = NSTextAlignmentLeft
        ;
//        priceLabel.text = @"¥999.99";
        self.priceLabel = priceLabel;
        [self addSubview:priceLabel];
        
        UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        buyButton.backgroundColor = [UIColor redColor];
        buyButton.titleLabel.textColor = [UIColor whiteColor];
        buyButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [buyButton setTitle:@"去结算(0)" forState:UIControlStateNormal];
        [buyButton addRadius:2];
        [buyButton addTarget:self action:@selector(buyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        self.buyButton = buyButton;
        [self addSubview:buyButton];
        
        UIView * topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0.5)];
        topLine.backgroundColor = [UIColor blackColor];
        [self addSubview:topLine];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
//    self.selectedButton.centerY = self.height * 0.5;
    self.selectedButton.x = 10;
    self.selectedButton.height = self.height*0.6;
    self.selectedButton.width = self.selectedButton.height;
    self.selectedButton.y = self.height * 0.5 - self.selectedButton.height * 0.5;
    
//    self.allSelectedLabel.centerY = self.selectedButton.centerY;
    self.allSelectedLabel.x = self.selectedButton.right;
    self.allSelectedLabel.width = [self.allSelectedLabel.text sizeInAre:MaxSize font:self.allSelectedLabel.font].width;
    self.allSelectedLabel.height = [self.allSelectedLabel.text sizeInAre:MaxSize font:self.allSelectedLabel.font].height;
    self.allSelectedLabel.y = self.selectedButton.y + self.allSelectedLabel.height * 0.25;
    
    self.buyButton.width = self.width*0.374;
    self.buyButton.height = self.height-0.5;
    self.buyButton.y = 0.5;
    self.buyButton.x = self.width-self.buyButton.width;
    
    self.totalLabel.width = [self.totalLabel.text sizeInAre:MaxSize font:self.totalLabel.font].width;
    self.totalLabel.height = [self.totalLabel.text sizeInAre:MaxSize font:self.totalLabel.font].height;
    self.totalLabel.x = self.allSelectedLabel.right + 5;
    self.totalLabel.centerY = self.height/2.0;
    
    self.priceLabel.width = self.buyButton.left - self.totalLabel.right - 10;
    self.priceLabel.height = self.totalLabel.height;
    self.priceLabel.centerY = self.totalLabel.centerY;
    self.priceLabel.x = self.totalLabel.right + 5;
}

-(void)setTotalPrice:(CGFloat)totalPrice{
    _totalPrice = totalPrice;
    self.totalLabel.text = [NSString stringWithFormat:@"%.2f",totalPrice];
    [self setNeedsLayout];
}

#pragma mark - 去结算按钮点击
- (void)buyButtonClick:(UIButton *)button
{
    if (self.handle) {
        self.handle(button);
    }
}

#pragma mark - 全选按钮点击
- (void)selectedButtonClick:(UIButton *)button
{
    self.selectedButton.selected = !self.selectedButton.selected;
    
    if (self.allSelected) {
        self.allSelected(button);
    }
}

@end
